package com.tianxin.workbao.workBao.Activity;

import android.content.Context;
import android.os.Bundle;
import androidx.annotation.Nullable;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import androidx.appcompat.content.res.AppCompatResources;
import androidx.appcompat.widget.Toolbar;
import android.util.AttributeSet;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.tianxin.workbao.workBao.R;
import com.tianxin.workbao.workBao.dataProvide.UserDataProvide;
import com.tianxin.workbao.workBao.listener.UpdateListViewListener;
import com.tianxin.workbao.workBao.utils.common.ToastUtils;

/**
 * Created by hacker on 2017/6/22.
 */

public class AlertPasswordActivity extends BaseActivity implements View.OnClickListener, UpdateListViewListener {

    private FloatingActionButton saveFab;
    private EditText oldPwdET;
    private EditText newPwdET;
    private EditText newPwdTwoET;
    private TextView alertInfoTV;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.alert_password);
        super.initStatusBar(true);

        initData();
        initView();
        initEvent();
        bindView();
    }

    @Override
    public View onCreateView(String name, Context context, AttributeSet attrs) {
        return super.onCreateView(name, context, attrs);
    }

    public void initView() {
        mToolbar = findViewById(R.id.toolbar);
        mToolbar.setTitle(getResources().getString(R.string.alter_pwd));
        setSupportActionBar(mToolbar);
        mToolbar.setNavigationIcon(AppCompatResources.getDrawable(this, R.drawable.ic_action_back_24dp));
        saveFab = findViewById(R.id.save_fab);
        oldPwdET = findViewById(R.id.old_pwd);
        newPwdET = findViewById(R.id.new_pwd);
        newPwdTwoET = findViewById(R.id.new_pwd_two);
        alertInfoTV = findViewById(R.id.alert_info);
    }

    @Override
    public void initData() {

    }


    @Override
    public void initEvent() {
        //navigationIcon event
        mToolbar.setNavigationOnClickListener(view -> AlertPasswordActivity.this.finish());
        saveFab.setOnClickListener(this);
    }


    @Override
    public void bindView() {
    }


    @Override
    public void onBackPressed() {
        //alert
        super.onBackPressed();
    }

    @Override
    public void onClick(View view) {
        int viewId = view.getId();
        switch (viewId) {
            case R.id.save_fab:
                alterPwd();
                break;
        }
    }

    private void alterPwd() {
        String oldPwd, newPwd, newPwdTwo, info;
        oldPwd = oldPwdET.getText().toString();
        if (oldPwd.length() < 6) {
            info = "旧密码的长度不能小于6位";
            setAlertInfoText(info);
            showMessage(info);
            oldPwdET.setText("");
            oldPwdET.setFocusable(true);
            return;
        }
        newPwd = newPwdET.getText().toString();
        if (newPwd.length() < 6) {
            info = "新密码的长度不能小于6位";
            setAlertInfoText(info);
            showMessage(info);
            newPwdET.setText("");
            newPwdET.setFocusable(true);
            return;
        }
        newPwdTwo = newPwdTwoET.getText().toString();
        if (newPwdTwoET.length() < 6) {
            info = "确认密码的长度不能小于6位";
            setAlertInfoText(info);
            showMessage(info);
            newPwdTwoET.setText("");
            newPwdTwoET.setFocusable(true);
            return;
        }
        if (!newPwd.equals(newPwdTwo)) {
            info = "两次输入的密码不一至";
            setAlertInfoText(info);
            showMessage(info);
            newPwdET.setText("");
            newPwdTwoET.setText("");
            newPwdET.setFocusable(true);
            return;
        }
        getUserDataProvide().alertPassword(oldPwd, newPwd);
    }

    private void setAlertInfoText(String info) {
        alertInfoTV.setText("");
        alertInfoTV.setText(info);
    }

    @Override
    public void showMessage(String msg) {
        if (msg == "ok") {
            super.quitApp();
        } else if (msg.contains("failure")) {
            setAlertInfoText("失败！" + msg.replace("failure", ""));
            ToastUtils.showShort("失败！" + msg.replace("failure", ""));
        } else {
            ToastUtils.showShort(msg);
        }
    }

    @Override
    public void showProgress() {
        getSweetAlertDialog(this).showDialog("", 1200);
    }

    @Override
    public void hideProgress() {
        getSweetAlertDialog(this).dismiss();
    }

    @Override
    public void onRefreshData(Object result) {

    }

    @Override
    public void onLoadMoreData(Object result) {

    }

    private UserDataProvide getUserDataProvide() {
        UserDataProvide provide = new UserDataProvide(this, this);
        return provide;
    }
}
